package ssm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import ssm.dao.Track;
import ssm.dao.TrackCar;
import ssm.dao.TrackPartnerCount;
import ssm.model.*;
import ssm.vo.PersonCount;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FollowServiceImpl implements FollowService {

    @Autowired
    private TrackMapper trackMapper;

    @Autowired
    private TrackCarMapper trackCarMapper;
    @Value("${spaceTime}")
    private int spaceTime;
    @Value("${ppl_high}")
    private float ppl_high;
    @Value("${ppl_middle}")
    private float ppl_middle;

    @Autowired
    private YktBaseUserMapper yktBaseUserMapper;

    @Autowired
    private TrackPartnerCountMapper trackPartnerCountMapper;

    public List<Map<String,Object>> perFellowPerson(String jid, String startTime,String endTime){
        List<Map<String,Object>>result=new ArrayList<Map<String, Object>>();
//        List<Map<String,Object>>result1=trackMapper.getPFPMess(jid,startTime,endTime);
        List<PersonCount> counts=trackPartnerCountMapper.selectByJidTime(jid,startTime,endTime);
//        List<TrackPartnerCount> trackPartnerCounts = trackPartnerCountMapper.selectByJid(jid, startTime, endTime);
//        if(trackPartnerCounts.isEmpty()){
//            return null;
//        }
//        for(TrackPartnerCount trackPartnerCount:trackPartnerCounts){
//            Map<String,Object>map=new HashMap<String,Object>();
//            map.put("date",trackPartnerCount.getDate());
//            map.put("num",trackPartnerCount.getNum());
//            if(trackPartnerCount.getKh1().equals(jid)){
//                map.put("xm",yktBaseUserMapper.selectXmByJid(trackPartnerCount.getKh2()));
//                map.put("xhgh",trackPartnerCount.getKh2());
//            }else{
//                map.put("xm",yktBaseUserMapper.selectXmByJid(trackPartnerCount.getKh1()));
//                map.put("xhgh",trackPartnerCount.getKh1());
//            }
//            result.add(map);
//        }
        if(counts.isEmpty()){
            return null;
        }
        for(PersonCount count:counts){
            Map<String,Object>map=new HashMap<String,Object>();
//            map.put("date","");
            map.put("num",count.getSum());
            map.put("xm",yktBaseUserMapper.selectXmByJid(count.getXhgh()));
            map.put("xhgh",count.getXhgh());
            result.add(map);
        }
        return result;

    }

    /*public List<Map> perFellowPerson(String jid, String starttime, String endtime) throws ParseException {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("jid", jid);
            map.put("starttime", starttime);
            map.put("endtime", endtime);
            //获取人跟随数据
            List<Track> tracks = trackMapper.getByTimeOrJid(map);
            Map<String, Map> maps = new HashMap<String, Map>();
            for (Track track : tracks) {
                System.out.println(track);
                String xhgh = track.getXhgh();
                String camerId = track.getSbbh();
                String personTime=track.getSj();
                int length = tracks.size();
                String bigTime = timeAdd(personTime, spaceTime);
                String smallTime = timeAdd(personTime, -spaceTime);
                List<Track> fellowPerson = trackMapper.getByTimeOrCamerId(camerId, bigTime, smallTime);
                addTracks_person(fellowPerson, maps, length, xhgh);
            }
            List<Map> list = new ArrayList<Map>();
            Set<String> keys = maps.keySet();
            for (String key : keys) {
                list.add(maps.get(key));
            }
            return list;
        }
    */
    public List<Map> carFellowCar(String cph, String starttime, String endtime) throws ParseException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("cph", cph);
        map.put("starttime", starttime);
        map.put("endtime", endtime);
        //获取人跟随数据
        List<TrackCar> cars = trackCarMapper.getByTimeOrCph(cph, starttime, endtime);
        Map<String, Map> maps = new HashMap<String, Map>();
        for (TrackCar car : cars) {
            String camerId = car.getSbbh();
            String personTime = car.getJgsj();
            int length = cars.size();
            String bigTime = timeAdd(personTime, spaceTime);
            String smallTime = timeAdd(personTime, -spaceTime);
            List<TrackCar> fellowCar = trackCarMapper.getByTimeOrCamerId(camerId, bigTime, smallTime);
            addTracks_car(fellowCar, maps, length, cph);
        }
        List<Map> list = new ArrayList<Map>();
        Set<String> keys = maps.keySet();
        for (String key : keys) {
            list.add(maps.get(key));
        }
        return list;
    }

    private String timeAdd(String time, int num) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt = sdf.parse(time);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.SECOND, num);//日期减1年
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }

    private void addTracks_person(List<Track> fellowPerson, Map<String, Map> maps, int length, String xhgh) {

        for (Track track : fellowPerson) {
            if (track.getXhgh().equals(xhgh)) {
                continue;
            }
            if (maps.get(track.getXhgh()) != null) {
                update_person(track, length, maps);
                System.out.println(maps);
            } else {
                add_person(track, length, maps);
                System.out.println(maps);
            }
        }
    }

    private void addTracks_car(List<TrackCar> fellowCar, Map<String, Map> maps, int length, String cph) {

        for (TrackCar car : fellowCar) {
            if (car.getCphm().equals(cph)) {
                continue;
            }
            if (maps.get(car.getCphm()) != null) {
                update_car(car, length, maps);
            } else {
                add_car(car, length, maps);
            }
        }
    }

    private void add_car(TrackCar car, int length, Map<String, Map> maps) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("carPlate", car.getCphm());
        map.put("matchCount", 1);
        map.put("plateColor", car.getCpys());
        String ppcd = (Integer) map.get("matchCount") / (length * 1.0) > ppl_high ? "高" : (Integer) map.get("matchCount") / (length * 1.0) > ppl_middle ? "中" : "低";
        map.put("matchrate", ppcd);
        maps.put(car.getCphm(), map);
    }

    private void update_car(TrackCar car, int length, Map<String, Map> maps) {
        Map<String, Object> map = maps.get(car.getCphm());
        map.put("matchCount", (Integer) map.get("matchCount") + 1);
        String ppcd = (Integer) map.get("matchCount") / (length * 1.0) > ppl_high ? "高" : (Integer) map.get("matchCount") / (length * 1.0) > ppl_middle ? "中" : "低";
        map.put("matchrate", ppcd);
        maps.put(car.getCphm(), map);
    }


    private void update_person(Track track, int length, Map<String, Map> maps) {
        Map<String, Object> map = maps.get(track.getXhgh());
        map.put("matchCount", (Integer) map.get("matchCount") + 1);
        String ppcd = (Integer) map.get("matchCount") / (length * 1.0) > ppl_high ? "高" : (Integer) map.get("matchCount") / (length * 1.0) > ppl_middle ? "中" : "低";
        map.put("matchrate", ppcd);
        maps.put(track.getXhgh(), map);
    }

    private void add_person(Track track, int length, Map<String, Map> maps) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", track.getXm());
        map.put("jid", track.getXhgh());
        map.put("matchCount", 1);
        String ppcd = (Integer) map.get("matchCount") / (length * 1.0) > ppl_high ? "高" : (Integer) map.get("matchCount") / (length * 1.0) > ppl_middle ? "中" : "低";
        map.put("matchrate", ppcd);
        maps.put(track.getXhgh(), map);
    }


}
